import cv2
import numpy as np


class DepthViewer:
    def __init__(self,img_depth,wd_size=15):
        self.img_depth = img_depth
        self.wd_size = wd_size

        
        self.img_color = cv2.applyColorMap(cv2.convertScaleAbs(img_depth, alpha=0.03), cv2.COLORMAP_JET)
        self.img_crop = cv2.resize(self.img_color[:30,:30],(30*self.wd_size,30*self.wd_size))

        

    def run(self):
        cv2.namedWindow('image')
        cv2.namedWindow('value')
        cv2.setMouseCallback('image', self.print_value)

        cv2.imshow("image", self.img_color)
        cv2.imshow("value", self.img_crop)
        while True:
            key = cv2.waitKey(1)
            if  key == ord("q"):
                break

    def print_value(self,event, x, y, flags, param):
        H,W = self.img_color.shape[:2]
        if event == cv2.EVENT_MOUSEMOVE:
            img_value = self.img_depth[y,x]
            x_min = x-15 if x-15 > 0 else 0
            x_max = x+15 if x+15 < W else W
            y_min = y-15 if y-15 > 0 else 0
            y_max = y+15 if y+15 < H else H
            img_crop = cv2.resize(self.img_color[y_min:y_max,x_min:x_max],(30*self.wd_size,30*self.wd_size))
            img_crop = cv2.rectangle(img_crop,
                (int(30*self.wd_size/2-self.wd_size),int(30*self.wd_size/2-self.wd_size)),
                (int(30*self.wd_size/2+self.wd_size),int(30*self.wd_size/2+self.wd_size)), 
                (11, 0, 83),2
            )
            cv2.setWindowTitle ('value',f"{img_value}")


if __name__ == "__main__":
    
    img_path = "data/test.npy"
    img_depth = np.load(img_path)

    depth_viewer = DepthViewer(img_depth, wd_size = 15)
    depth_viewer.run()








